package com.eaoa.uc.busorder.form;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.eaoa.base.EABaseForm;
import com.eaoa.vo.bus.BusCargo;
import com.eaoa.vo.bus.BusOrder;
import com.eaoa.vo.bus.BusTransfer;
import com.eaoa.vo.pub.PubCustomer;
import com.stony.core.util.CoreUtils;

public class BusOrderForm extends EABaseForm{

	// primary key
	private String orderno;

	// fields
	private String billcode;
	private String ordertype;
	private String fromaddressno;
	private String fromaddressname;
	private String fromaddressdetail;
	private String fromaddressprovince;
	private String fromaddresscity;
	private String fromaddressdistrict;
	private String fromdate;
	private String fromdatehour;
	private String fromman;
	private String frommanphone;
	private String toaddressno;
	private String toaddressname;
	private String toaddressdetail;
	private String toaddressprovince;
	private String toaddresscity;
	private String toaddressdistrict;
	private String todate;
	private String todatehour;
	private String toman;
	private String tomanphone;
	private String tolastdate;
	private String tolastdatehour;
	private String tostorebillcode;
	private String topickupisself;
	private String proxycustomleter;
	private String paymethod;
	private String isinsure;
	private String goodsvalue;
	private String remarks;
	private String islock;
	private String billstatus;
	private String transferno;
	private String customerno;
	private String customername;
	
	// ////////// modify lines
	private BusCargoForm[] cargos;
	private String linescount="0";
	
	public void setVO(BusOrder vo){

		this.setOrderno(CoreUtils.formatString(vo.getOrderno()));
		this.setBillcode(vo.getBillcode());
		this.setOrdertype(vo.getOrdertype());
		this.setFromaddressno(CoreUtils.formatString(vo.getFromaddressno()));
		this.setFromaddressname(vo.getFromaddressname());
		this.setFromaddressdetail(vo.getFromaddressdetail());
		this.setFromaddressprovince(vo.getFromaddressprovince());
		this.setFromaddresscity(vo.getFromaddresscity());
		this.setFromaddressdistrict(vo.getFromaddressdistrict());
		this.setFromdate(CoreUtils.formatString(vo.getFromdate()));
		this.setFromdatehour(CoreUtils.formatTime(vo.getFromdate()));
		this.setFromman(vo.getFromman());
		this.setFrommanphone(vo.getFrommanphone());
		
		this.setToaddressno(CoreUtils.formatString(vo.getToaddressno()));
		this.setToaddressname(vo.getToaddressname());
		this.setToaddressdetail(vo.getToaddressdetail());
		this.setToaddressprovince(vo.getToaddressprovince());
		this.setToaddresscity(vo.getToaddresscity());
		this.setToaddressdistrict(vo.getToaddressdistrict());
		this.setTodate(CoreUtils.formatString(vo.getTodate()));
		this.setTodatehour(CoreUtils.formatTime(vo.getTodate()));
		this.setToman(vo.getToman());
		this.setTomanphone(vo.getTomanphone());
		
		this.setTolastdate(CoreUtils.formatString(vo.getTolastdate()));
		this.setTolastdatehour(CoreUtils.formatTime(vo.getTolastdate()));
		this.setTostorebillcode(vo.getTostorebillcode());
		this.setTopickupisself(vo.getTopickupisself());
		this.setProxycustomleter(vo.getProxycustomleter());
		this.setPaymethod(vo.getPaymethod());
		this.setIsinsure(vo.getIsinsure());
		this.setGoodsvalue(CoreUtils.formatString(vo.getGoodsvalue()));
		this.setRemarks(vo.getRemarks());
		this.setIslock(vo.getIslock());
		this.setBillstatus(vo.getBillstatus());
		
		if(vo.getBusTransfer()!=null)
			this.setTransferno(CoreUtils.formatString(vo.getBusTransfer().getTransferno()));
		if(vo.getPubCustomer()!=null){
			this.setCustomerno(CoreUtils.formatString(vo.getPubCustomer().getCustomerno()));
			this.setCustomername(vo.getPubCustomer().getNameshort()+"-"+vo.getPubCustomer().getName());
		}
			

	}
	
	public BusOrder getVO(){
		BusOrder po = new BusOrder();
		
		po.setOrderno(CoreUtils.parseLong(this.getOrderno()));
		po.setBillcode(this.getBillcode());
		po.setOrdertype(this.getOrdertype());
		po.setFromaddressno(CoreUtils.parseLong(this.getFromaddressno()));
		po.setFromaddressname(this.getFromaddressname());
		po.setFromaddressdetail(this.getFromaddressdetail());
		po.setFromaddressprovince(this.getFromaddressprovince());
		po.setFromaddresscity(this.getFromaddresscity());
		po.setFromaddressdistrict(this.getFromaddressdistrict());
		po.setFromdate(CoreUtils.parseTime(this.getFromdate() +" "+ this.getFromdatehour()));
		po.setFromman(this.getFromman());
		po.setFrommanphone(this.getFrommanphone());
		
		po.setToaddressno(CoreUtils.parseLong(this.getToaddressno()));
		po.setToaddressname(this.getToaddressname());
		po.setToaddressdetail(this.getToaddressdetail());
		po.setToaddressprovince(this.getToaddressprovince());
		po.setToaddresscity(this.getToaddresscity());
		po.setToaddressdistrict(this.getToaddressdistrict());
		po.setTodate(CoreUtils.parseTime(this.getTodate() +" "+ this.getTodatehour()));
		po.setToman(this.getToman());
		po.setTomanphone(this.getTomanphone());
		
		po.setTolastdate(CoreUtils.parseTime(this.getTolastdate() +" "+ this.getTolastdatehour()));
		po.setTostorebillcode(this.getTostorebillcode());
		po.setTopickupisself(this.getTopickupisself());
		po.setProxycustomleter(this.getProxycustomleter());
		po.setPaymethod(this.getPaymethod());
		po.setIsinsure(this.getIsinsure());
		po.setGoodsvalue(CoreUtils.parseBigDecimal(this.getGoodsvalue()));
		po.setRemarks(this.getRemarks());
		po.setIslock(this.getIslock());
		po.setBillstatus(this.getBillstatus());

		
		if(NumberUtils.isNumber(this.getTransferno()))
			po.setBusTransfer(new BusTransfer(CoreUtils.parseLong(this.getTransferno())));
		if(NumberUtils.isNumber(this.getCustomerno()))
			po.setPubCustomer(new PubCustomer(CoreUtils.parseLong(this.getCustomerno())));
		
		return po;
	}
	
	
	
	public String getOrderno() {
		return orderno;
	}
	public void setOrderno(String orderno) {
		this.orderno = orderno;
	}
	public String getBillcode() {
		return billcode;
	}
	public void setBillcode(String billcode) {
		this.billcode = billcode;
	}
	public String getOrdertype() {
		return ordertype;
	}
	public void setOrdertype(String ordertype) {
		this.ordertype = ordertype;
	}
	public String getFromaddressno() {
		return fromaddressno;
	}
	public void setFromaddressno(String fromaddressno) {
		this.fromaddressno = fromaddressno;
	}
	public String getFromaddressname() {
		return fromaddressname;
	}
	public void setFromaddressname(String fromaddressname) {
		this.fromaddressname = fromaddressname;
	}
	public String getFromaddressprovince() {
		return fromaddressprovince;
	}
	public void setFromaddressprovince(String fromaddressprovince) {
		this.fromaddressprovince = fromaddressprovince;
	}
	public String getFromaddresscity() {
		return fromaddresscity;
	}
	public void setFromaddresscity(String fromaddresscity) {
		this.fromaddresscity = fromaddresscity;
	}
	public String getFromaddressdistrict() {
		return fromaddressdistrict;
	}
	public void setFromaddressdistrict(String fromaddressdistrict) {
		this.fromaddressdistrict = fromaddressdistrict;
	}
	public String getFromdate() {
		return fromdate;
	}
	public void setFromdate(String fromdate) {
		this.fromdate = fromdate;
	}
	public String getFromman() {
		return fromman;
	}
	public void setFromman(String fromman) {
		this.fromman = fromman;
	}
	public String getFrommanphone() {
		return frommanphone;
	}
	public void setFrommanphone(String frommanphone) {
		this.frommanphone = frommanphone;
	}
	public String getToaddressno() {
		return toaddressno;
	}
	public void setToaddressno(String toaddressno) {
		this.toaddressno = toaddressno;
	}
	public String getToaddressname() {
		return toaddressname;
	}
	public void setToaddressname(String toaddressname) {
		this.toaddressname = toaddressname;
	}
	public String getToaddressprovince() {
		return toaddressprovince;
	}
	public void setToaddressprovince(String toaddressprovince) {
		this.toaddressprovince = toaddressprovince;
	}
	public String getToaddresscity() {
		return toaddresscity;
	}
	public void setToaddresscity(String toaddresscity) {
		this.toaddresscity = toaddresscity;
	}
	public String getToaddressdistrict() {
		return toaddressdistrict;
	}
	public void setToaddressdistrict(String toaddressdistrict) {
		this.toaddressdistrict = toaddressdistrict;
	}
	public String getTodate() {
		return todate;
	}
	public void setTodate(String todate) {
		this.todate = todate;
	}
	public String getToman() {
		return toman;
	}
	public void setToman(String toman) {
		this.toman = toman;
	}
	public String getTomanphone() {
		return tomanphone;
	}
	public void setTomanphone(String tomanphone) {
		this.tomanphone = tomanphone;
	}
	public String getTolastdate() {
		return tolastdate;
	}
	public void setTolastdate(String tolastdate) {
		this.tolastdate = tolastdate;
	}
	public String getTostorebillcode() {
		return tostorebillcode;
	}
	public void setTostorebillcode(String tostorebillcode) {
		this.tostorebillcode = tostorebillcode;
	}
	public String getTopickupisself() {
		return topickupisself;
	}
	public void setTopickupisself(String topickupisself) {
		this.topickupisself = topickupisself;
	}
	public String getProxycustomleter() {
		return proxycustomleter;
	}
	public void setProxycustomleter(String proxycustomleter) {
		this.proxycustomleter = proxycustomleter;
	}
	public String getPaymethod() {
		return paymethod;
	}
	public void setPaymethod(String paymethod) {
		this.paymethod = paymethod;
	}
	public String getIsinsure() {
		return isinsure;
	}
	public void setIsinsure(String isinsure) {
		this.isinsure = isinsure;
	}
	public String getGoodsvalue() {
		return goodsvalue;
	}
	public void setGoodsvalue(String goodsvalue) {
		this.goodsvalue = goodsvalue;
	}
	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	public String getIslock() {
		return islock;
	}
	public void setIslock(String islock) {
		this.islock = islock;
	}
	public String getBillstatus() {
		return billstatus;
	}
	public void setBillstatus(String billstatus) {
		this.billstatus = billstatus;
	}

	public String getTransferno() {
		return transferno;
	}
	public void setTransferno(String transferno) {
		this.transferno = transferno;
	}
	public String getCustomerno() {
		return customerno;
	}
	public void setCustomerno(String customerno) {
		this.customerno = customerno;
	}

	public String getCustomername() {
		return customername;
	}

	public void setCustomername(String customername) {
		this.customername = customername;
	}
	
	public String getFromaddressdetail() {
		return fromaddressdetail;
	}

	public void setFromaddressdetail(String fromaddressdetail) {
		this.fromaddressdetail = fromaddressdetail;
	}

	public String getToaddressdetail() {
		return toaddressdetail;
	}

	public void setToaddressdetail(String toaddressdetail) {
		this.toaddressdetail = toaddressdetail;
	}

	public String getFromdatehour() {
		return fromdatehour;
	}

	public void setFromdatehour(String fromdatehour) {
		this.fromdatehour = fromdatehour;
	}

	public String getTodatehour() {
		return todatehour;
	}

	public void setTodatehour(String todatehour) {
		this.todatehour = todatehour;
	}

	public String getTolastdatehour() {
		return tolastdatehour;
	}

	public void setTolastdatehour(String tolastdatehour) {
		this.tolastdatehour = tolastdatehour;
	}

	// //////////////////
	public BusCargoForm[] getCargos() {
		return cargos;
	}

	public void setCargos(BusCargoForm[] cargos) {
		this.cargos = cargos;
	}

	public String getLinescount() {
		return linescount;
	}

	public void setLinescount(String linescount) {
		this.linescount = linescount;
	}

	public List getBusCargosVO() {
		List ret = new ArrayList();
		for (int i = 0; cargos != null && i < cargos.length; i++) {
			ret.add(cargos[i].getVO());
		}
		return ret;
	}

	public void setBusCargosVO(List cargoVOs) {
		linescount = CoreUtils.formatString(new Long(cargoVOs.size()));
		cargos = new BusCargoForm[cargoVOs.size()];
		for (int i = 0; i < cargoVOs.size(); i++) {
			BusCargo cargo = (BusCargo) cargoVOs.get(i);
			cargos[i] = new BusCargoForm();
			cargos[i].setVO(cargo);
		}
	}
	

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);
		String linescount = request.getParameter("linescount");
		if (StringUtils.isNotBlank(linescount)) {
			cargos = new BusCargoForm[Integer
					.parseInt(linescount)];
			for (int i = 0; i < Integer.parseInt(linescount); i++) {
				cargos[i] = new BusCargoForm();
			}
		}
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = super.validate(mapping, request);
		if (cargos != null) {
			for (int i = 0; i < this.cargos.length; i++) {
				if (StringUtils.isBlank(this.cargos[i].getCargoname())) {
					ActionMessage error = new ActionMessage("errors.required",
							getMessages("busCargoForm.cargoname",
									new String[] { "" + (i + 1) }));
					errors.add("cargos[" + i + "].cargoname", error);
				}
				if (StringUtils.isBlank(this.cargos[i].getPackagetype())) {
					ActionMessage error = new ActionMessage("errors.required",
							getMessages("busCargoForm.packagetype",
									new String[] { "" + (i + 1) }));
					errors.add("cargos[" + i + "].itemfee", error);
				}
				if (!CoreUtils.isNumber(this.cargos[i].getNumber()
						.trim())) {
					ActionMessage error = new ActionMessage("errors.double",
							getMessages("busCargoForm.number",
									new String[] { "" + (i + 1) }));
					errors.add("cargos[" + i + "].number", error);
				}
			}
		}
		return errors;
	}


	
}
